import { getCache, setCache, removeCache } from '@/utils/session';
import { login, logout, getCodeTest } from '@/api/user';
import { resetRouter } from '@/router';

const state = {
  accountInfo: getCache('USERINFO') || '',
  token: getCache('TOKEN') || ''
};

const mutations = {
  SET_TOKEN(state, token) {
    state.token = token;
  },
  SET_USERINFO(state, userInfo) {
    state.accountInfo = userInfo;
  }
};

const actions = {
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      // 开发环境模拟登录
      if (process.env.NODE_ENV === 'development') {
        const mockData = {
          token: 'dev-token',
          userInfo: {
            username: userInfo.username,
            role: '1,2,3,4,5,6,7,8,20,21,22,23,24,25,26,11,12,13,14,15,16' // 所有权限
          }
        };
        commit('SET_TOKEN', mockData.token);
        setCache('TOKEN', mockData.token);
        commit('SET_USERINFO', mockData.userInfo);
        setCache('USERINFO', mockData.userInfo);
        resolve(mockData);
        return;
      }

      // 生产环境正常登录
      login(userInfo)
        .then(res => {
          const { data } = res;
          if (data) {
            commit('SET_TOKEN', data.token);
            setCache('TOKEN', data.token);
            commit('SET_USERINFO', data.userInfo);
            setCache('USERINFO', data.userInfo);
          }
          resolve(data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  codeTest({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      // 开发环境模拟登录
      if (process.env.NODE_ENV === 'development') {
        const mockData = {
          token: 'dev-token',
          userInfo: {
            phone: userInfo.phone,
            role: '1,2,3,4,5,6,7,8,20,21,22,23,24,25,26,11,12,13,14,15,16' // 所有权限
          }
        };
        commit('SET_TOKEN', mockData.token);
        setCache('TOKEN', mockData.token);
        commit('SET_USERINFO', mockData.userInfo);
        setCache('USERINFO', mockData.userInfo);
        resolve(mockData);
        return;
      }

      // 生产环境正常登录
      getCodeTest(userInfo)
        .then(res => {
          const { data } = res;
          if (data) {
            commit('SET_TOKEN', data.token);
            setCache('TOKEN', data.token);
            commit('SET_USERINFO', data.userInfo);
            setCache('USERINFO', data.userInfo);
          }
          resolve(data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  logout({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '');
      commit('SET_USERINFO', '');
      removeCache('TOKEN');
      removeCache('USERINFO');
      resetRouter();
      resolve();
    });
  },

  updateInfo({ commit }, userInfo) {
    return new Promise((resolve) => {
      commit('SET_USERINFO', userInfo);
      setCache('USERINFO', userInfo);
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
